#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "./genometools-1.6.0.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

if [[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]]; then echo "请输入正确的构建路径"; exit 1; fi
if [[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]]; then echo "请输入正确的安装路径"; exit 1; fi
[[ "$1" = "$2" ]] && echo "构建路径与安装路径不能相同" && exit 1
buildpath=$1
installpath=$2
if [[ "$1" =~ .*/$ ]]; then buildpath=${buildpath%/*}; fi
if [[ "$2" =~ .*/$ ]]; then installpath=${installpath%/*}; fi

wget -t 30 -c -P $buildpath https://github.com/genometools/genometools/archive/v1.6.0.tar.gz -O $buildpath/gt-1.6.0.tar.gz
cd $buildpath
tar xf gt-1.6.0.tar.gz
cd genometools-1.6.0
sed -i -e 's/g++/clang++/g' -e 's/gcc/clang/g' Makefile
make -j 8 cairo=no && make cairo=no prefix=$installpath/genometools install
make cleanup
cat>"$installpath/genometools/genometools_modulefiles"<<EOF
#%Module1.0
conflict genometools
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set genometools \$pwd
setenv genometools \$genometools
prepend-path PATH \$genometools/bin
EOF
